import { createStore } from 'vuex';
import { MessageBo, OllamaTag } from '@/interface/home';
import { getOllamaModel } from '@/utils/api';

interface State {
  messages: MessageBo[];
  resizeObserver: MutationObserver | null;
  ollamaModel: string;
}

export default createStore({
  state: {
    messages: [] as MessageBo[],
    resizeObserver: null,
    ollamaModel: '',
  },
  mutations: {
    /**
     * @description 更新消息
     * @param {State} state
     * @param {message: MessageBo; index: number} payload
     * @returns void
     */
    updateMessages(state: State, payload: { message: MessageBo; index?: number }) {
      const { message, index } = payload;
      if (index !== undefined) {
        state.messages[index] = message;
      } else {
        state.messages.push(message);
      }
    },
    /**
     * @description 设置ollama模型
     * @param {State} state
     * @param {string} ollamaModel
     * @returns void
     */
    setOllamaModel(state: State, ollamaModel: string) {
      state.ollamaModel = ollamaModel;
    },
  },
  actions: {
    /**
     * @description 获取ollama模型
     * @param commit
     * @returns Promise<void>
     */
    async getOllamaModels({ commit }): Promise<void> {
      const ollamaModel: OllamaTag[] = await getOllamaModel();
      commit('setOllamaModel', ollamaModel[0].model);
    },
  },
});
